package dave.hibernate.lab;


import java.io.File;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

//import utilities.module.DataParser;
import utilities.DataParser4;
//import utilities.module.KiacSimulator;

//import column.mappings.UserAnnotated;

public class HibernateUtil {
	private static SessionFactory factory;
	public static Configuration getInitializedConfiguration(){
		Configuration config = new Configuration();
		//add all JPA annotated classes here
		config.addAnnotatedClass(Customer.class);
		//config.addAnnotatedClass(User.class);
		//config.addAnnotatedClass(Temporary_CAD.class);
		//config.addAnnotatedClass(Temporary_CAD.class);
		config.configure();
		return config;
	}//end getInitializedConfiguration
	
	public static Session getSession(){
		if(factory == null){
			Configuration cfg = getInitializedConfiguration(); 
			factory = cfg.buildSessionFactory();
		}
		Session session = factory.getCurrentSession();
		return session;
	}//end GetSession
	
	public void closeSession(){
		getSession().close();
	}
	
	public static void recreateDatabase(){
		Configuration cfg = getInitializedConfiguration();
		new SchemaExport(cfg).create(true, true);
	}
	
	public static Session beginTransaction(){
		Session session = getSession();
		session.beginTransaction();
		return session;
	}
	
	public static void commitTransaction(){
		getSession().getTransaction().commit();
	}
	
	public static void rollbackTransaction(){
		getSession().getTransaction().rollback();
	}
	
	public static void storeCustomer(String fName, String lName, String address, String tel){
		Session session = HibernateUtil.beginTransaction();
		Customer customer = new Customer();
		//user.setPassword("abc123");
		/*customer.setFirstName("David");
		customer.setLastName("Goh");
		customer.setAddress("2242 No Way St, Memphis, Tn 38134");
		customer.setTel("901-901-9011");
*/		
		customer.setFirstName(fName);
		customer.setLastName(lName);
		customer.setAddress(address);
		customer.setTel(tel);
		
		session.saveOrUpdate(customer);
		//session.saveOrUpdate(user);
		System.out.println("user saved");
		HibernateUtil.commitTransaction();
		System.out.println("transaction successful");
	}//end storeCustomer
	
	public static void main(String[] args){
		String fileDir, fileName;
		fileDir="/Users/CIOMAC/Documents/Customer/" ;
		//fileName = "C:\\Users\\MyselfW\\customer1.txt";
		//HibernateUtil.recreateDatabase();
		//Session session = HibernateUtil.beginTransaction();
		//System.out.println("creating user");
		//User user = new User();
		File dir = new File(fileDir);
		File[] files = dir.listFiles();
		for(File f: files){
			fileName = fileDir + f.getName();
			//Pause for 1 seconds
            //Thread.sleep(1000);

			Session session = HibernateUtil.beginTransaction();
			Customer customer = new Customer();
			//user.setPassword("abc123");
			/*customer.setFirstName("David");
			customer.setLastName("Goh");
			customer.setAddress("2242 No Way St, Memphis, Tn 38134");
			customer.setTel("901-901-9011");
	*/		
			customer.setFirstName(DataParser4.getFirstName(fileName));
			customer.setLastName(DataParser4.getLastName(fileName));
			customer.setAddress(DataParser4.getAddress(fileName));
			customer.setTel(DataParser4.getTel(fileName));
			
			session.saveOrUpdate(customer);
			//session.saveOrUpdate(user);
			System.out.println("user saved");
			HibernateUtil.commitTransaction();
			System.out.println("transaction successful");
		}//end for loop
		
	}//end main

}//end class